package comp418.blingo;

/**
 * Coding by liu jingbang, HK POLYU
 */
import java.io.IOException;
import java.io.ObjectOutputStream;

import messagePackage.MessageFlag;
import messagePackage.SendingMessage;
import android.util.Log;

public class SocketWriter extends Thread {
	private String userID;
	private boolean finished=true;
	ObjectOutputStream ObjectOS = null;
	SendingMessage writeMessage=null;
	
	SocketWriter(ObjectOutputStream ObjectOS){
		this.ObjectOS = ObjectOS;
		writeMessage=null;
		
	}
	
	protected void setUserID(String userID){
		this.userID=userID;
	}
	/**
	 * Call this funcion if we need to Sign Up
	 * @param username
	 * @param password
	 * @param gender
	 */
	protected void setLogin(String username, String password){
		
		writeMessage = new SendingMessage();
		writeMessage.password=password;
		writeMessage.name=username;
		writeMessage.flag=MessageFlag.LOG_IN_T;
		
	}
	
	protected void setSignup(String username, String password){
		if(!finished)
			return; 
		writeMessage = new SendingMessage();
		writeMessage.password=password;
		writeMessage.name=username;
		writeMessage.flag=MessageFlag.SIGN_UP_T;
		
	}
	
	
	
	protected void setUserprofile(String username){
		if(!finished)
			return; 
		else 
			finished = !finished;
		writeMessage = new SendingMessage();
		writeMessage.name=username;
		writeMessage.flag=MessageFlag.VIEW_PROFILE_T;
	}
	/**
	 * 
	 * @param content
	 * @param token
	 */
	protected void setUpdating(String username){
		if(!finished)
			return;
		else 
			finished = !finished;
		writeMessage.name=username;
		
		writeMessage.flag=MessageFlag.UPDATE_PROFILE_T;
		
	}
	/**
	 * Call this function if we need to send the "ASRL" or ....
	 * @param content
	 */
	protected void setQuestionList(){
		if(!finished)
			return;
		else 
			finished = !finished;
		writeMessage = new SendingMessage();
		writeMessage.flag=MessageFlag.REFRESH_T;
		
	}
	/**
	 * Call this function if open a question
	 * @param content
	 */
	protected void setQuestion(int id){
		if(!finished)
			return;
		else 
			finished = !finished;
		writeMessage = new SendingMessage();
		writeMessage.flag=MessageFlag.OPEN_QUESTION_T;
		writeMessage.questionID=id;
		
	}
	protected void setQuestionClear(){
		writeMessage=new SendingMessage();
		writeMessage.flag=MessageFlag.OPEN_QUESTION_T;
		
		
	}
	/**
	 * Call this function if the the Server need the USERID
	 */
	
	protected void setCreateQuestion(String title, String content,String username){
		if(!finished)
			return;
		else 
			finished = !finished;
		writeMessage = new SendingMessage();
		writeMessage.name=username; 
		writeMessage.questionTitle=title;
		writeMessage.textContent=content;
		writeMessage.flag=MessageFlag.RAISE_QUESTION_T;
		
	}
	
	protected void setCreateAnswer(int qid, String username,String content){
		if(!finished)
			return;
		else 
			finished = !finished;
		writeMessage = new SendingMessage();
		writeMessage.name=username;
		writeMessage.questionID=qid;
		writeMessage.textContent=content;
		writeMessage.flag=MessageFlag.ANSWER_T;
		
	}
	
	/**
	 * Call this function if the the Server need the USERID
	 */
	
	protected void setCoin(int qid){
		if(!finished)
			return;
		else 
			finished = !finished;
		writeMessage = new SendingMessage();
		
		writeMessage.questionID=qid;
		Log.i("c",new Integer(qid).toString());
		writeMessage.flag=MessageFlag.REWARD_T;
		this.run();
	}
  public void run() {
     
    	try {
    		if(writeMessage!=null)
			ObjectOS.writeObject(writeMessage);
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        this.interrupt();
    }
	 
}
